package cn.tedu.oop;

/*本类用来测试构造方法*/
/*
初始化完成之后赋值，还是在堆内存中赋值
 */
public class TestConstructor {
    public static void main(String[] args) {
        //创建person类p进行测试
        Person p = new Person();
        Person p2 = new Person("海绵宝宝");
        Person p3 = new Person("葫芦娃",99,"花果山水帘洞");
        System.out.println(p3);
        System.out.println(p.getAddress());
        System.out.println(p.getName());
        System.out.println(p.getAddress());
        System.out.println(p.getAge());
        p.eat();

    }

}
//1.抽象封装成一个类组件
class Person {
    //2.属性----成员变量
    private String name;//姓名
    private int age;//年龄
    private String address;//地址值

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                '}';
    }

    /*1.默认存在无参构造器，当new person（）创建对象时，会默认触发此无参构造*/
    //定义格式为  修饰符+类名（形参可有可无）{}
    //6.创建一个无参构造
    /*构造方法也存在重载现象 如果添加了含参构造默认的无参构造就会被覆盖，注意要手动添加无参构造*/
    public Person(){

        System.out.println("我是Person类的无参构造");
    }
    //创建有参构造
    public Person(String name){
        System.out.println("我是person类的有参构造");
    }
    //创建全参构造

    public Person(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
        System.out.println("我是person类的全参构造");
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    //3.行为----方法
    public void eat(){
        System.out.println("要记得吃早饭~~~~~~~~");
    }
}

